package com.qiaoba.module.system.controller;

import com.github.pagehelper.PageHelper;
import com.qiaoba.api.log.annotation.Log;
import com.qiaoba.api.log.enums.BusinessType;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.PageResult;
import com.qiaoba.module.system.entity.SysDictType;
import com.qiaoba.module.system.entity.dto.SysDictTypeDTO;
import com.qiaoba.module.system.entity.param.SysDictTypeParam;
import com.qiaoba.module.system.service.SysDictDataService;
import com.qiaoba.module.system.service.SysDictTypeService;
import com.qiaoba.poi.excel.util.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 字典类型管理 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2022-09-22 04:20:28
 */
@Validated
@RestController
@RequestMapping("/system/dict/type")
@RequiredArgsConstructor
@Tag(name = "字典类型")
public class SysDictTypeController {

    private final SysDictTypeService sysDictTypeService;
    private final SysDictDataService sysDictDataService;

    @PreAuthorize("hasAuthority('system:dict:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public PageResult<SysDictType> list(@Validated @ParameterObject SysDictTypeParam param, @ParameterObject PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<SysDictType> list = sysDictTypeService.selectList(param);
        return PageResult.build(list);
    }

    @PreAuthorize("hasAuthority('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    @Operation(summary = "获取详情")
    public Result<SysDictType> getInfo(@Size(max = 20, message = "ID不合法") @PathVariable String dictId) {
        return Result.success(sysDictTypeService.selectById(dictId));
    }

    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('system:dict:add')")
    @PostMapping
    @Operation(summary = "新增")
    public Result add(@Validated(AddGroup.class) @RequestBody SysDictTypeDTO dto) {
        return Result.toAjax(sysDictTypeService.insert(dto));
    }

    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('system:dict:edit')")
    @PutMapping
    @Operation(summary = "修改")
    public Result edit(@Validated(EditGroup.class) @RequestBody SysDictTypeDTO dto) {
        return Result.toAjax(sysDictTypeService.updateById(dto));
    }

    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('system:dict:remove')")
    @DeleteMapping("/{ids}")
    @Operation(summary = "批量删除")
    public Result remove(@PathVariable List<String> ids) {
        return Result.toAjax(sysDictTypeService.deleteByIds(ids));
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PreAuthorize("hasAuthority('system:dict:export')")
    @PostMapping("/export")
    @Operation(summary = "导出Excel")
    public void export(HttpServletResponse response, @Validated SysDictTypeParam param) {
        List<SysDictType> list = sysDictTypeService.selectList(param);
        ExcelUtil.exportExcel(list, SysDictType.class, "字典类型", response);
    }

    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('system:dict:remove')")
    @DeleteMapping("/refresh-cache")
    @Operation(summary = "刷新缓存")
    public Result refreshCache() {
        sysDictDataService.resetDictCache();
        return Result.success();
    }

    @GetMapping("/option-select")
    @Operation(summary = "获取字典下拉框列表")
    public Result<List<SysDictType>> optionSelect() {
        List<SysDictType> dictTypes = sysDictTypeService.selectList(new SysDictTypeParam());
        return Result.success(dictTypes);
    }
}
